/* 
 * @(#)sensitiveDaoImpl.java    Created on Apr 19, 2013
 * Copyright (c) 2013 ZDSoft Networks, Inc. All rights reserved.
 * $Id$
 */
package com.leruan.aasimp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.SensitiveDao;
import com.leruan.aasimp.entity.Sensitive;
import com.leruan.aasimp.entitydto.SensitiveDto;

public class sensitiveDaoImpl extends HibernateDaoSupport implements SensitiveDao {

    public void insertSensitive(Sensitive sensitive) {
        Session session = this.getSession();
        session.save(sensitive);
    }

    public List<SensitiveDto> findSensitive(String boardId, String sensitiveString, int pageNo) {
        Session session = this.getSession();

        StringBuffer sb = new StringBuffer("select f.userId, f.username,s.time,b.boardName,s.sensitivestring from "
                + "Sensitive s,Board b,Forumuser f where s.userId=f.userId and s.boardId = b.boardId and"
                + " b.boardId= ? ");
        if (!"".equals(sensitiveString)) {
            sb.append("and s.sensitivestring like '%" + sensitiveString + "%'");
        }
        sb.append(" order by s.time desc");
        Query query = session.createQuery(sb.toString());
        int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
        query.setString(0, boardId);
        query.setFirstResult(first);
        query.setMaxResults(UsedCarConstant.PAGE_SIZE);

        List list = query.list();

        List<SensitiveDto> sensitiveList = new ArrayList<SensitiveDto>();
        for (int i = 0; i < list.size(); i++) {
            SensitiveDto dto = new SensitiveDto();
            Object[] object = (Object[]) list.get(i);
            dto.setUserId((String) object[0]);
            dto.setUsername((String) object[1]);
            dto.setTime((Date) object[2]);
            dto.setBoardName((String) object[3]);
            dto.setSensitivestring((String) object[4]);
            sensitiveList.add(dto);
        }
        return sensitiveList;
    }

    public Long findSensitiveTotal(Map<String, String> map) {
        Session session = this.getSession();
        boolean flag = true;
        StringBuffer sb = new StringBuffer("select count(id) from Sensitive ");
        String boardId = map.get("boardId");
        String sensitiveString = map.get("sensitiveString");
        if (!"".equals(boardId) || null != boardId) {
            sb.append("where boardId =?");
            flag = false;
        }
        if (!"".equals(sensitiveString) || null != sensitiveString) {
            if (flag) {
                sb.append("where sensitivestring like '%" + sensitiveString + "%'");
            }
            else {
                sb.append("and sensitivestring like '%" + sensitiveString + "%'");
            }
        }
        Query query = session.createQuery(sb.toString());
        if (!"".equals(boardId) || null != boardId) {
            query.setString(0, boardId);
        }
        return (Long) query.uniqueResult();
    }
}
